/*
 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicNameValuePair.java $
 * $Revision: 604625 $
 * $Date: 2007-12-16 06:11:11 -0800 (Sun, 16 Dec 2007) $
 *
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package com.koushikdutta.ion.sample;

import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.utils.net.Uri;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 资源文件处理工具类
 */
public class ResourceUtils {
    private static final Logger logger = Logger.getLogger(ResourceUtils.class.getName());

    /**
     * 获取rawFile 文件夹内的资源文件
     *
     * @param context 上下文
     * @param path     资源文件绝对路径 resources/rawfile/telescope.jpg
     * @param fileName 资源文件名 telescope.jpg
     * @return 文件结果
     */
    public static File getResourceRawFile(Context context, String path, String fileName) {
        RawFileEntry rawFileEntry = context.getResourceManager().getRawFileEntry(path);
        File file = null;
        Resource resource = null;
        FileOutputStream fos = null;
        if (rawFileEntry != null) {
            try {
                resource = rawFileEntry.openRawFile();
                file = new File(context.getCacheDir().getParent() + File.separator + fileName);
                fos = new FileOutputStream(file);
                byte[] b = new byte[1024];
                int length;
                while ((length = resource.read(b)) != -1) {
                    fos.write(b, 0, length);
                }
            } catch (IOException e) {
                logger.log(Level.INFO, "IOException in test");
            } finally {
                try {
                    if (resource != null) {
                        resource.close();
                    }
                } catch (IOException e) {
                    logger.log(Level.INFO, "IOException in test");
                }

                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    logger.log(Level.INFO, "IOException in test");
                }
            }
        }
        return file;
    }

    /**
     * 资源文件id转Uri
     *
     * @param context 上下文
     * @param resId 资源id
     * @return uri 结果
     */
    public static Uri getUriByResId(Context context, int resId) {
        return Uri.parse("dataability:///".concat(context.getBundleName()).concat("/").concat(String.valueOf(resId)));
    }
}
